package com.linkhub.oms.component.aliyunoss.config;

import com.aliyun.oss.ClientBuilderConfiguration;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.common.auth.CredentialsProvider;
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
import com.linkhub.oms.component.aliyunoss.config.properties.AliyunOssProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Description: OssClientConfig
 * @Author: echo
 * @Date: 2024/5/8 17:17
 * @Version: 1.0
 */
@Configuration
@EnableConfigurationProperties(AliyunOssProperties.class)
public class OssClientConfig {


    @Bean
    public OSS ossClient(AliyunOssProperties ossProperties) {
        // 使用代码嵌入的RAM用户的访问密钥配置访问凭证。
        CredentialsProvider credentialsProvider = new DefaultCredentialProvider(ossProperties.getAccessKeyId(),
                ossProperties.getAccessKeySecret());
        // 创建ClientBuilderConfiguration实例，您可以根据实际情况修改默认参数。
        ClientBuilderConfiguration conf = new ClientBuilderConfiguration();
        // 设置是否支持CNAME。CNAME用于将自定义域名绑定到目标Bucket。
        if (ossProperties.isCustomDomain()) {
            conf.setSupportCname(true);
        }
        // 创建OSSClient实例。
        return new OSSClientBuilder().build(ossProperties.getEndpoint(), credentialsProvider, conf);
    }

}
